package com.shiyue.cqe.service.common;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.shiyue.cqe.bind.xml.bean.Option;
import com.shiyue.cqe.bind.xml.bean.Select;
import com.shiyue.cqe.component.model.ColumnComponent;
import com.shiyue.cqe.component.model.ColumnType;

public class TableHelper {
	public final static String createTableHead = "CREATE TABLE :tableName(:columns constraint pk_connectdb primary key(:pk));";
	public static Map<String,Select> selects;
	
	public static Map<String, Select> getSelects() {
		return selects;
	}

	public static void setSelects(Map<String, Select> selects) {
		TableHelper.selects = selects;
	}

	public static String buildCreateTableSql(String tableName,List<ColumnComponent> columnComponents){
		if(columnComponents == null || columnComponents.isEmpty()){
			return null;
		}
		String primary = "";
		StringBuilder columns = new StringBuilder();
		for(ColumnComponent column : columnComponents){
			if(column.isPrimary()){
				primary = column.getName();
			}
			columns.append(column.getName().toUpperCase()).append(" ")
				.append(getOracleColumnType(Enum.valueOf(ColumnType.class, column.getType().toUpperCase()),column.getLength()))
				.append(" ");
			if(StringUtils.isNotBlank(column.getDefalutValue())){
				columns.append("DEFAULT '").append(column.getDefalutValue()).append("' ");
			}
			if(column.isNotNull()){
				columns.append("NOT NULL ENABLE");
			}
			if(StringUtils.isNotBlank(column.getDescription())){
				columns.append(",--").append(column.getDescription()).append("\r\n");
			}else{
				columns.append(",");
			}
		}
		String sql = createTableHead.replaceAll(":tableName", tableName.toUpperCase());
		sql = sql.replaceAll(":pk", primary.toUpperCase());
		return sql.replaceAll(":columns", columns.toString());
	}
	
	/**
	 * 获取oracle数据类型
	 * @param columnType
	 * @param length 当为varchar时 需要传入长度
	 * @return  default return VARCHAR2(255 BYTE)
	 */
	public static String getOracleColumnType(ColumnType columnType,int length){
		switch(columnType){
			case LONG:
				return "NUMBER(19,0)";
			case INT:
				return "NUMBER(8,0)";
			case VARCHAR:
				return "VARCHAR2(" + length + " BYTE)";
			case DATE:
				return "TIMESTAMP (6)";
			default:
				return "VARCHAR2(255 BYTE)";
		}
	}
	
	/**
	 * 
	 * @param inputType input  类型
	 * @param value 值
	 * @param inputAttribute  input附加属性 例如：name="" length="" id=""
	 * @param selectName 如果是select 需要传入selectName 来在select的xml中来选择 对应的select
	 * @return
	 */
	public static String getInput(InputType inputType,String value,String inputAttribute,String selectName){
		switch(inputType){
			case TEXT:
				return "<input type='text' value='" + value + "' " + inputAttribute + "/>";
			case TEXTAREA:
				return "<textarea " + inputAttribute + ">" + value + "</textarea>";
			case SELECT:
				return "<select " + inputAttribute + ">" + componentSelect(selectName,value) + "</select>";
			default:
				return "<input type='text' value='" + value + "' " + inputAttribute + "/>";
		}
	}
	
	private static String componentSelect(String selectName,String value){
		Select select = selects.get(selectName);
		if(select == null){
			return "";
		}
		StringBuilder sb = new StringBuilder();
		if(select.isBlank()){
			sb.append("<option value='' ></option>");
		}
		for(Option option : select.getOptions()){
			sb.append("<option ");
			if(StringUtils.equals(option.getValue(), value)){
				sb.append(" selected='selected'");
			}
			sb.append(" value='").append(option.getValue()).append("' >").append(option.getText()).append("</option>");
		}
		return sb.toString();
	}
}
